//app.js
App({
  onLaunch: function() {
    // wx.setStorageSync('car', []);
    var userData = wx.getStorageSync("userinfo");
    // if (userData.openId) {
    //   return;
    // }
    var self = this;
    wx.login({
      success: function(login_res) {
        wx.getUserInfo({
          success: function(user_res) {
            let serverUrl = self.globalData.serverUrl + '/addUser';
            //写入数据库
            wx.request({
              url: serverUrl,
              data: {
                nickName: user_res.userInfo.nickName,
                avatarUrl: user_res.userInfo.avatarUrl,
                sex: user_res.userInfo.gender,
                code: login_res.code
              },
              method: 'POST',
              success: function(result) {
                var userInfo = {
                  openId: result.data.data,
                  nickName: user_res.userInfo.nickName
                }
                wx.setStorageSync("userinfo", userInfo)
              }
            })
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    serverUrl: 'http://localhost:8888'
  }
})